﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace WorkflowAutomation
{
    class LogHelper
    {
		private static readonly string LogFile = Path.Combine(Environment.CurrentDirectory, "execution.log");

        /// <summary>
        /// Logs a message to log file.
        /// </summary>
        /// <param name="message"></param>
		public static void LogMessage(string message)
		{
			try
			{
				TextWriterTraceListener listener;
				using (listener = new TextWriterTraceListener(LogFile))
				{
					listener.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + "    " + message);
					listener.Flush();
					listener.Close();
				}
			}
			catch
			{
				//do nothing
			}
		}

        /// <summary>
        /// Logs error message from Exception object.
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
		public static void LogErrorMessage(string message, Exception ex)
		{
			try
			{
				TextWriterTraceListener listener;
				using (listener = new TextWriterTraceListener(LogFile))
				{
					listener.WriteLine(DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss") + "    " + message + ex.Message);
					if (ex.InnerException != null)
					{
						listener.WriteLine(ex.InnerException.Message);
					}
					listener.Flush();
					listener.Close();
				}
			}
			catch
			{
				//do nothing
			}
		}
    }
}
